package com.cht.commonUtil.deployment;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jboss.as.cli.CommandContext;
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.dmr.ModelNode;

import com.cht.commonUtil.webInit.SP;
import com.cht.commonUtil.widgets.Widgets;
import com.google.gson.Gson;

public class GetCandidateReloadList extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding( SP.UIcodeSet );
		response.setCharacterEncoding( SP.UIcodeSet );
		response.setContentType("application/json");
		
		String sql = "SELECT cip,cport,cid,cpwd FROM mpsquadenv WHERE subsysId=? AND envId=? AND squadId=?";
		String depFileString = request.getParameter("depFileString");
		String envId = request.getParameter("envId");
		String serverGroupId = request.getParameter("serverGroupId");
		String subsysId = "fOrder";
		List<String> reloadList = new ArrayList<String>();
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		CommandContext cmdCtx = null; 
		ModelControllerClient client = null;
		try {
			conn = Widgets.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, subsysId);
			ps.setString(2, envId);
			ps.setString(3, serverGroupId);
			rs = ps.executeQuery();
			rs.next();
			
			String cip = rs.getString("cip");
			int cport = rs.getInt("cport");
			String cid = rs.getString("cid");
			char[] cpwd = rs.getString("cpwd").toCharArray();
			
			cmdCtx = Widgets.initCommandContext(cip, cport, cid, cpwd);
			client = cmdCtx.getModelControllerClient();
			ModelNode requestNode = cmdCtx.buildRequest(":read-resource(recursive=true)");
			ModelNode result = client.execute(requestNode);
			List<ModelNode> filesOnAP = result.get("result").get("server-group").get( serverGroupId ).get("deployment").asList();
			
			for (int i=0;i<filesOnAP.size();i++) {
				String name = filesOnAP.get(i).get(0).get("name").toString().replaceAll("\"", "");
				if( !depFileString.contains(name) ) {
					reloadList.add(name);	
				}
			}
		} catch(Exception e) {
			Widgets.errLog(e);
		} finally {
			if (client != null) { client.close(); }
			if (cmdCtx != null) {
				cmdCtx.disconnectController();
				cmdCtx.terminateSession();
			}
			try {
				if (rs != null) {rs.close();}
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) {
				System.out.println(Widgets.errProcess(e));
			}
		}
		
		response.getWriter().write( new Gson().toJson( reloadList ) );
	}

}
